import { validateAddress, validateDeviceId } from "@/utils/rules";

const filterHandle = (vm, h, params, demo) => {
  const { row } = { ...params };

  return h("div", [
    h(
      "Button",
      {
        props: {
          type: "info",
          size: "small",
          disabled: demo || row.company == "长沙平和堂五一广场店"
        },
        style: {
          marginRight: "5px"
        },
        on: {
          click: () => {
            vm.$router.push({
              path: "/temperature/device/" + row.id
            });
          }
        }
      },
      "设备详情"
    ),
    h(
      "Button",
      {
        props: {
          type: "warning",
          size: "small",
          disabled: demo || row.company == "长沙平和堂五一广场店"
        },
        style: {
          marginRight: "5px"
        },
        on: {
          click: () => {
            vm.handleEdit(row);
          }
        }
      },
      "编辑"
    ),
    h(
      "Poptip",
      {
        props: {
          confirm: true,
          title: "您确定要删除这条数据吗?",
          transfer: true,
          cancelText: "取消"
        },
        on: {
          "on-ok": () => {
            vm.handleDelete(row.id);
          }
        }
      },
      [
        h(
          "Tooltip",
          {
            props: {
              content: "删除"
            }
          },
          [
            h(
              "Button",
              {
                props: {
                  type: "error",
                  size: "small",
                  disabled: demo || row.company == "长沙平和堂五一广场店"
                }
              },
              "删除"
            )
          ]
        )
      ]
    )
  ]);
};

export const deviceColumns = (vm, demo) => {
  return [
    {
      title: "是否在线",
      align: "center",
      render: (h, params) => {
        if (params.row.onlineState) {
          return h(
            "Tag",
            {
              props: {
                color: "green"
              }
            },
            "在线"
          );
        }
        return h(
          "Tag",
          {
            props: {
              color: "red"
            }
          },
          "离线"
        );
      }
    },
    {
      title: "状态",
      align: "center",
      render: (h, params) => {
        const { deviceStatus } = params.row;
        switch (deviceStatus) {
          case 0:
            return h(
              "Tag",
              {
                props: {
                  color: "default"
                }
              },
              "未知"
            );
          case 1:
            return h(
              "Tag",
              {
                props: {
                  color: "success"
                }
              },
              "正常"
            );
          case 2:
            return h(
              "Tag",
              {
                props: {
                  color: "error"
                }
              },
              "告警"
            );
          default:
            return h(
              "Tag",
              {
                props: {
                  color: "default"
                }
              },
              "未知"
            );
        }
      }
    },
    {
      title: "设备ID",
      align: "center",
      key: "deviceId"
    },
    {
      title: "设备名称",
      align: "center",
      key: "deviceName"
    },
    {
      title: "设备类型",
      align: "center",
      key: "deviceTypeDes"
    },
    {
      title: "所属企业",
      align: "center",
      key: "companyName"
    },
    {
      title: "更新时间",
      align: "center",
      render: (h, params) =>
        h("span", vm.moment(params.row.updatedAt).format("YYYY-MM-DD HH:mm:ss"))
    },
    {
      title: "操作",
      align: "center",
      width: 200,
      render: (h, params) => filterHandle(vm, h, params, demo)
    }
  ];
};

export const addForm = {
  companyId: "",
  groupId: "",
  deviceType: "",
  deviceName: "",
  deviceAlias: "",
  location: "",
  imei: null,
  imsi: null,
  deviceId: "",
  onetId: "",
  enableState: false
};

export const rulesAddForm = {
  companyId: [
    {
      required: true,
      message: "请选择所属客户"
    }
  ],
  groupId: [
    {
      required: true,
      message: "请选择所属区域"
    }
  ],
  deviceType: [
    {
      required: true,
      message: "请选择设备类型"
    }
  ],
  deviceName: [
    {
      required: true,
      validator: validateAddress(
        "设备名称",
        "设备名称只能输入中文、数字、字母、下划线、中划线"
      ),
      trigger: "blur"
    }
  ],
  deviceAlias: [
    {
      required: true,
      validator: validateAddress(
        "设备别名",
        "设备别名只能输入中文、数字、字母、下划线、中划线"
      ),
      trigger: "blur"
    }
  ],
  location: [
    {
      required: true,
      validator: validateAddress(
        "设备位置",
        "设备位置只能输入中文、数字、字母、下划线、中划线"
      ),
      trigger: "blur"
    }
  ],
  imei: [
    {
      required: true,
      validator: validateDeviceId("IMEI", "IMEI只能输入正整数"),
      trigger: "blur"
    }
  ],
  imsi: [
    {
      required: true,
      validator: validateDeviceId("IMSI", "IMSI只能输入正整数"),
      trigger: "blur"
    }
  ],
  deviceId: [
    {
      required: true,
      validator: validateDeviceId("设备ID", "设备ID只能输入数字"),
      trigger: "blur"
    },
    {
      min: 12,
      message: "设备ID不得少于12个字符"
    }
  ],
  onetId: [
    {
      required: true,
      validator: validateDeviceId("ONENETID", "ONENETID只能输入数字"),
      trigger: "blur"
    },
    {
      max: 9,
      message: "ONENETID长度不能超过9位数"
    }
  ],
  enableState: [
    {
      required: true
    }
  ]
};
